<?php
//====================================================
//		FileName: action.address.php
//		Summary:  通讯录信息
//====================================================
header("Content-type: text/html; charset=utf-8");
if(!defined('CORE'))exit("error!"); 

	
//列表
if($do==""){
	If_rabc($action,$do); //检测权限	
	//判断检索值		
	
	if($_POST['kewords']){$search .= " and name like '%".strip_tags($_POST[kewords])."%'";}			
	if($_POST['time_start']!="" && $_POST['time_over']!=""){
		$search .= " and `created_at` >  '".strtotime($_POST[time_start]." 00:00:00")."' AND  `created_at` <  '".strtotime($_POST[time_over] ." 23:59:59")."'";
	}
	
	//设置分页
	if($_POST[numPerPage]==""){$numPerPage="10";}else{$numPerPage=$_POST[numPerPage];}
	if($_GET[pageNum]==""||$_GET[pageNum]=="0" ){$pageNum="0";}else{$pageNum=($_GET[pageNum]-1)*$numPerPage;}
	$num=mysql_query("SELECT * FROM address_list where 1=1 $search");//当前频道条数
	$total=mysql_num_rows($num);//总条数	
	$page=new page(array('total'=>$total,'perpage'=>$numPerPage));

	//查询
	$sql="SELECT * FROM address_list  where 1=1 $search order by id desc LIMIT $pageNum,$numPerPage";
	
	//echo $sql;
	$db->query($sql);
	$list=$db->fetchAll();

	//模版
	$smt = new smarty();smarty_cfg($smt);
	$smt->assign('list',$list);
	$smt->assign('numPerPage',$_POST[numPerPage]); //显示条数
	$smt->assign('pageNum',$_GET[pageNum]); //当前页数
	$smt->assign('total',$total);  //总条数
	$smt->assign ('page',$page->show());
	$smt->assign('title',"好友");
	$smt->display('adress_list.htm');
	exit;	
}

//新建	
if($do=="new"){	
	If_rabc($action,$do); //检测权限
	$smt = new smarty();smarty_cfg($smt);

	//模版	
	$smt->assign('title',"添加");
	$smt->display('adress_new.htm');
	exit;
}

//写入
if($do=="add"){
	If_rabc($action,$do); //检测权限
	
	//if(!$_POST[url]){echo error($msg);exit;}
	$created_at = time();
	$sql="INSERT INTO address_list (name,sex,deparyment,position,tel,address,`created_at`)VALUES('$_POST[name]','$_POST[sex]','$_POST[deparyment]','$_POST[position]','$_POST[tel]','$_POST[address]','$created_at')";	
	if($db->query($sql)){echo success($msg,"?action=address");}else{echo error($msg);}
	exit;
}


//编辑	
if($do=="edit"){
	If_rabc($action,$do); //检测权限
	$smt = new smarty();smarty_cfg($smt);
	//查询
	$sql=" SELECT * FROM address_list where id='{$id}' LIMIT 1";
	$db->query($sql);
	$row=$db->fetchRow();
	
	//模版
	$smt->assign('row',$row);
	$smt->assign('title',"编辑");
	$smt->display('address_edit.htm');
	exit;
}

//更新
if($do=="updata"){
	If_rabc($action,$do); //检测权限	
	
	if(!$_POST[id]){echo error($msg);exit;}
	$updated_at= time();
	$sql="UPDATE address_list SET 
	`name` = '$_POST[name]',
	`sex` = '$_POST[sex]',
	`tel` = '$_POST[tel]',
	`address` = '$_POST[address]',
	`updated_at` = '$updated_at' WHERE `id` ='$_POST[id]' LIMIT 1 ;";
	
	if($db->query($sql)){echo success($msg,"?action=address");}else{echo error($msg);}	
	exit;
}

//删除
if($do=="del"){
	If_rabc($action,$do); //检测权限
	$sql="delete from `address_list` where `id`=$id limit 1";
	if($db->query($sql)){echo success($msg,"?action=address");}else{echo error($msg);}		
	exit;
}
?>